import TableLyout from '@/components/commonPublic/tableLyout.vue'
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import lineEcharts from "@/components/machineNetworkManage/analysisDataManage/concreteQuality/lineEcharts.vue"; //折线图
import barEcharts from "@/components/machineNetworkManage/analysisDataManage/concreteQuality/barEcharts.vue"; //柱状图
import messageBox from "@/components/public/messageBox"; //主页面分页
import actions from "@/actions";

import formJs from "./regexDetail.js"; //新增弹框配置
import {
    getTestHntQualityTrendsList, //列表页接口
    getBaseDataitemDetailByItemCode, //字典
    getUserCompanyTreeList, //树状图
} from "@/api/request.js";


export default {
    name: "concreteQuality",
    data() {
        return {
            name: "concreteQuality",
            userNature: null, //用户Nature
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,
            projectsObj: [], //项目名
            tendersObj: [],
            falgId: null,
            isLock: false,
            searchObj: formJs.searchObj, //搜索条件
            loading: false,
            btnObj: [], //按钮
            tableData: {}, //全部数据
            // 图表
            groupNmu: 0,
            getList: {
                MachineId: "",
                fCementtype: "",
                fCloseoperation: "",
                keyWord: "",
                modelType: "0",
                page: 1,
                pageSize: 15,

                fprojectid: "",
                fCompanyid: "", // 左侧选中组织机构id
                keyType: "", // 上方查询条件试验类型
                fAge: "", //上方查询条件龄期
                fStrength: "", //上方查询条件强度等级
                userId: "", //用户id
                startDates: "", //开始时间
                endDates: "", //结束时间
                timeDates: "",
                upNum: "", //波动上限
                downNum: "", //波动下限
                taskNumber: "", //区段
            },
            limitation: {
                title: "波动上下限",
                upperLimit: '1.5',
                upperText: "请输入上限",
                lowerLimit: '0.9',
                lowerText: "请输入下限"
            },
            lineChartList: [{
                    con1: "类别",
                    con2: "",
                    con3: "",
                    con4: ""
                },
                {
                    con1: "组数",
                    con2: "",
                    con3: "",
                    con4: ""
                },
                {
                    con1: "占比(%)",
                    con2: "",
                    con3: "",
                    con4: ""
                },
            ],
            // 折线图
            lineChartData: null, //折线图数据
            echartsTitle: "", //折线图标题
            xName: "时间", //X轴名称
            yName: "(MPa)", //Y轴名称
            // 折线图


            Histogram: {
                title: "区段",
                section: '0.9,1.0,1.15,1.25,1.5',
                sectionText: '可填多个，用英文","隔开',
            },
            histogramWidth: "",
            HistogramChartList: {},
            // 柱状图
            barChartData: null, //数据
            barChartTitle: "", //标题
            xBarName: "", //X轴名称
            yBarName: "组数", //Y轴名称
            // 柱状图
            // 图表
        };
    },
    components: {
        TableLyout,
        publicForm,
        messageBox,
        lineEcharts,
        barEcharts
    },
    mounted() {
        actions.onGlobalStateChange((state, prev) => {
            this.userInfo = prev.currentUser
        }, true);

        this.lineChartList[0].con2 = "<" + this.limitation.lowerLimit + "倍组数";
        this.lineChartList[0].con3 = this.limitation.lowerLimit + "-" + this.limitation.upperLimit + "倍组数";
        this.lineChartList[0].con4 = ">" + this.limitation.upperLimit + "倍组数";

        this.getList = {
            MachineId: "",
            fCementtype: "",
            fCloseoperation: "",
            keyWord: "",
            modelType: "",
            page: 1,
            pageSize: 15,
            fprojectid: "",
            fCompanyid: "", //左侧选中组织机构id
            keyType: "抗压强度", //上方查询条件材料种类
            fAge: "", //上方查询条件龄期
            fStrength: "", //上方查询条件强度等级
            upNum: this.limitation.upperLimit, //波动上限
            downNum: this.limitation.lowerLimit, //波动下限
            taskNumber: this.Histogram.section, //区段
            userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
            startDates: "", //进场开始时间
            endDates: "", //进场结束时间
            timeDates: [],
        };
        this.getStrengthgrade("StrengthGrade1"); //强度
        this.getSpecimenage("SpecimenAge1"); //龄期


    },
    methods: {

        getBtnName(name, status) {
            console.log(name, status);
        },

        // 图表
        clickInput(e) {
            this.limitation = e;
            this.lineChartList[0].con2 = "<" + this.limitation.lowerLimit + "倍组数";
            this.lineChartList[0].con3 = this.limitation.lowerLimit + "-" + this.limitation.upperLimit + "倍组数";
            this.lineChartList[0].con4 = ">" + this.limitation.upperLimit + "倍组数";
            this.getList.upNum = this.limitation.upperLimit;
            this.getList.downNum = this.limitation.lowerLimit;
            this.yj_searchForm(this.getList);
        },
        clickHistogram(e) {
            this.Histogram = e;
            this.getList.taskNumber = this.Histogram.section;
            this.yj_searchForm(this.getList);
        },
        // 图表
        confirmMsg() {
            this.showMsgBox = false;
        },
        reset(e) {
            this.getList = this.$options.data.call(this).getList
            this.getList.fprojectid = e
        },
        // 选择项目名称
        selectCompany(e) {
            this.companyName = e.F_ProjectName;
            this.getList.fDepartmentid = e.F_ProjectId;
            this.projectName = "";
            this.getList.fCompanyid = "";


            this.yj_searchForm(this.getList);
        },
        // 选择施工单位
        selectProject(e) {

            this.projectName = e.F_CompanyName;
            this.getList.fCompanyid = e.F_CompanyId;
            // this.yj_searchForm(this.getList);
        },
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                    headers: {
                        "Content-Type": "application/json",
                        Authorization: localStorage.getItem("access_token"),
                    },
                }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.tendersObj = [];
                    if (data.length == 0) {
                        this.$message.warning({
                            content: "有其他人员登录此账号！",
                            duration: 1000,
                        });
                        setTimeout(() => {
                            // window.location.href = "http://172.18.5.201:1080/login";
                        }, 1200);
                    } else if (data.length == 1) {
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                        this.getList.fCompanyid = data[0].sectionId;
                    } else {
                        this.tendersObj = [{
                            fItemName: "所有标段",
                            fItemValue: "",
                        }, ];
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                    }
                }
            });
        },
        selectTender(e) {
            this.getList.stationId = "";
            this.getList.machineId = "";
            this.bhzDataList = [];
            this.bhjDataList = [];

        },
        // 获取列表数据
        yj_searchForm(data) {
            if (data.fAge == "龄期(天)") {
                data.fAge = "";
            }
            window.sessionStorage.setItem(
                "GxEmbedded-machineNetworkManage_getList",
                JSON.stringify(data)
            );

            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            var obj = {
                fCompanyid: id, // 左侧选中组织机构id
                keyType: data.keyType, // 上方查询条件试验类型
                fAge: data.fAge, //上方查询条件龄期
                fStrength: data.fStrength, //上方查询条件强度等级
                userId: this.userInfo.id, //用户id
                startDates: data.startDates, //开始时间
                endDates: data.endDates, //结束时间
                upNum: data.upNum, //波动上限
                downNum: data.downNum, //波动下限
                taskNumber: data.taskNumber, //区段
            }
            this.echartsTitle = "";
            this.barChartTitle = "";
            this.groupNmu = 0
            this.loading = true;
            getTestHntQualityTrendsList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.echartsTitle = obj.startDates + "至" + obj.endDates + "【" + obj.fStrength + "】混凝土质量波动图（" + obj.keyType + ")";
                        that.barChartTitle = obj.startDates + "至" + obj.endDates + "【" + obj.fStrength + "】混凝土强度分布直方图（" + obj.keyType + ")";
                        that.tableData = res.data;

                        // 折线区域数据
                        that.lineChartData = {
                            xitems: [], //试验日期  X
                            yitems: [], //最大力强度  Y
                            fSamplenostr: [], //试件编号
                            fProjectlocation: [], //工程部位
                            downNum: [], //下限
                            upNum: [], //上限
                        }
                        if (res.data.HntQualityTrendsList.length != 0) {
                            that.groupNmu = res.data.HntQualityTrendsList.length;
                            res.data.HntQualityTrendsList.map(item => {
                                that.lineChartData.xitems.push(item.fTestdates);
                                that.lineChartData.yitems.push(item.fStrengthmeasurement);
                                that.lineChartData.fSamplenostr.push(item.fSamplenostr);
                                that.lineChartData.fProjectlocation.push(item.fProjectlocation);
                                that.lineChartData.downNum.push(item.downNum);
                                that.lineChartData.upNum.push(item.upNum);
                            })
                        }
                        that.lineChartList[1].con2 = res.data.downnum;
                        that.lineChartList[1].con3 = res.data.midnum;
                        that.lineChartList[1].con4 = res.data.upnum;
                        that.lineChartList[2].con2 = res.data.downnumPercent;
                        that.lineChartList[2].con3 = res.data.midnumPercent;
                        that.lineChartList[2].con4 = res.data.upnumPercent;

                        // 折线区域数据


                        //柱状图区域
                        var HistogramCharArr = [{
                            name1: "类别1",
                            name2: "类别2",
                            value: "组数",
                            percent: "占比(%)"
                        }, ];

                        var category = []; //类别
                        var category2 = []; //类别2  X
                        var groupNum = []; //组数  Y
                        var proportion = []; //占比(%)

                        HistogramCharArr.concat(res.data.itemslist).map(item => {
                            category.push(item.name1);
                            category2.push(item.name2);
                            groupNum.push(item.value);
                            proportion.push(item.percent);
                        }); //柱状图数据
                        that.HistogramChartList = {
                            category,
                            groupNum,
                            proportion
                        };
                        that.histogramWidth = (100 / that.HistogramChartList.category.length) + "%";

                        var xitems = category2.slice(2, category2.length);
                        var yitems = groupNum.slice(2, groupNum.length);
                        that.barChartData = {
                            xitems,
                            yitems
                        };
                        //柱状图区域

                    } else {
                        this.$message.error({
                            content: "列表数据获取失败" + res.msg,
                            duration: 1000
                        });
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    console.log("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        selectChange(e, a, b) {
            console.log(e, a, b);
            if (b == 'fStrength') {


                this.echartsTitle = this.getList.startDates + "至" + this.getList.endDates + "【" + e + "】混凝土质量波动图（" + this.getList.keyType + ")";
                this.barChartTitle = this.getList.startDates + "至" + this.getList.endDates + "【" + e + "】混凝土强度分布直方图（" + this.getList.keyType + ")";
            } else if (b == "keyType") {
                this.echartsTitle = this.getList.startDates + "至" + this.getList.endDates + "【" + this.getList.fStrength + "】混凝土质量波动图（" + e + ")";
                this.barChartTitle = this.getList.startDates + "至" + this.getList.endDates + "【" + this.getList.fStrength + "】混凝土强度分布直方图（" + e + ")";
            }
        },
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0]; // 开始时间
                this.getList.endDates = e[1]; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]]
            this.yj_searchForm(this.getList);
        },
        getDate() {
            if (JSON.parse(sessionStorage.getItem("GxEmbedded-machineNetworkManage_getList"))) {
                var getList = JSON.parse(sessionStorage.getItem("GxEmbedded-machineNetworkManage_getList"));
                // var id;
                // if (getList.fCompanyid != "") {
                //     id = getList.fCompanyid;
                // } else {
                //     id = getList.fprojectid;
                // }
                this.getList.fprojectid = getList.fprojectid;
                this.getList.fCompanyid = getList.fCompanyid; //左侧选中组织机构id
                this.getList.startDates = getList.startDates; // 开始时间
                this.getList.endDates = getList.endDates; // 截至时间
                this.getList.timeDates = getList.timeDates;
            } else {
                this.getList.startDates = this.common.getDay(-90); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [this.getList.startDates, this.getList.endDates];
            }
        },
        // 字典项
        // 强度等级
        getStrengthgrade(data) {
            getBaseDataitemDetailByItemCode({
                    fItemcode: data
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.searchObj[4].options = [];
                        res.data.baseDataitemdetailList.map((item) => {
                            this.searchObj[4].options.push({
                                value: item.fItemname,
                                label: item.fItemvalue,
                            });
                        });
                        this.getList.fStrength = this.searchObj[4].options[0].label;
                        this.getDate();

                    } else {
                        this.$message.error({
                            content: "强度等级" + res.msg,
                            duration: 1000
                        });
                    }
                })
                .catch(() => {
                    console.log("字典--强度等级接口报错");
                });
        },
        //试件龄期
        getSpecimenage(data) {
            getBaseDataitemDetailByItemCode({
                    fItemcode: data
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.searchObj[5].options = [{
                            value: "龄期(天)",
                            label: "龄期(天)"
                        }];
                        res.data.baseDataitemdetailList.map((item) => {
                            this.searchObj[5].options.push({
                                value: item.fItemname,
                                label: item.fItemvalue,
                            });
                        });
                        this.getList.fAge = this.searchObj[5].options[0].label;

                    } else {
                        this.$message.error({
                            content: "试件龄期" + res.msg,
                            duration: 1000
                        });
                    }
                })
                .catch(() => {
                    console.log("字典--试件龄期接口报错");
                });
        },
        // 字典项
    },
};